package com.hdw.med.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hdw.common.util.SnowflakeIdWorker;
import com.hdw.common.vo.PaginationVO;
import com.hdw.med.api.BaseDbTpcompService;
import com.hdw.med.bean.model.BaseDbTpcomp;
import com.hdw.med.mapper.BaseDbTpcompMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * baseDbTpcomp服务实现类
 * Created by hozeData on 2021/05/28.
 */
@Service
@Transactional
public class BaseDbTpcompServiceImpl implements BaseDbTpcompService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BaseDbTpcompServiceImpl.class);

    @Autowired
    private BaseDbTpcompMapper baseDbTpcompMapper;

    @Override
    public int insert(BaseDbTpcomp baseDbTpcomp) {
        if(baseDbTpcomp.getIdDbTpcomp()==null){
            Long uuid32 = SnowflakeIdWorker.getId();
            baseDbTpcomp.setIdDbTpcomp(uuid32);
        }
        return baseDbTpcompMapper.insert(baseDbTpcomp);
    }

    @Override
    public int insertSelective(BaseDbTpcomp baseDbTpcomp)  {
        if(baseDbTpcomp.getIdDbTpcomp()==null){
            Long uuid32 = SnowflakeIdWorker.getId();
            baseDbTpcomp.setIdDbTpcomp(uuid32);
        }
        return baseDbTpcompMapper.insertSelective(baseDbTpcomp);
    }

    @Override
    public int deleteById(Long idDbTpcomp)  {
        return baseDbTpcompMapper.deleteByPrimaryKey(idDbTpcomp);
    }

    @Override
    public int updateByIdSelective(BaseDbTpcomp baseDbTpcomp)  {

        return baseDbTpcompMapper.updateByPrimaryKeySelective(baseDbTpcomp);
    }

    @Override
    public int updateById(BaseDbTpcomp baseDbTpcomp)  {
        return baseDbTpcompMapper.updateByPrimaryKey(baseDbTpcomp);
    }

    @Override
    public BaseDbTpcomp getById(Long idDbTpcomp)  {
        return baseDbTpcompMapper.selectByPrimaryKey(idDbTpcomp);
    }

    @Override
    public List<BaseDbTpcomp> findByModel(BaseDbTpcomp baseDbTpcomp)  {
        return baseDbTpcompMapper.findByModel(baseDbTpcomp);
    }

    @Override
    public PaginationVO<BaseDbTpcomp> findPageByModel(BaseDbTpcomp baseDbTpcomp, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum == null? 1 : pageNum, pageSize == null? 10 : pageSize);
        List<BaseDbTpcomp> quesList = baseDbTpcompMapper.findByModel(baseDbTpcomp);
        PageInfo<BaseDbTpcomp> pageInfo = new PageInfo<>(quesList);
        PaginationVO<BaseDbTpcomp> paginationVo = new PaginationVO(pageInfo.getTotal(), pageInfo.getList());
        return paginationVo;
    }

    @Override
    public List<BaseDbTpcomp> findByDbType(String dbType) {
        BaseDbTpcomp tpcomp = new BaseDbTpcomp();
        tpcomp.setEuDbtp(dbType);
        return this.findByModel(tpcomp);
    }

    @Override
    public Map<Integer, BaseDbTpcomp> findByDbTypeMap(String dbType) {
        BaseDbTpcomp tpcomp = new BaseDbTpcomp();
        tpcomp.setEuDbtp(dbType);
        List<BaseDbTpcomp> list = this.findByModel(tpcomp);
        Map<Integer,BaseDbTpcomp> map = new HashMap<>();
        for(BaseDbTpcomp dbTpcomp:list) {
            map.put(dbTpcomp.getEuJavatp(),dbTpcomp);
        }
        return map;
    }

    @Override
    public Map<Integer, String> findByDbType2(String dbType) {
        BaseDbTpcomp tpcomp = new BaseDbTpcomp();
        tpcomp.setEuDbtp(dbType);
        List<BaseDbTpcomp> list = this.findByModel(tpcomp);
        Map<Integer,String> map = new HashMap<>();
        for(BaseDbTpcomp dbTpcomp:list) {
            map.put(dbTpcomp.getEuJavatp(),dbTpcomp.getEuFldtp());
        }
        return map;
    }
}
